<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\Attr;
use app\common\model\Brand;
use app\common\model\Cate;
use app\common\model\Spec;
use app\common\model\SpecValue;
use think\exception\HttpException;

class Common
{
    public function findCate (Cate $cate){
        try {
            $pid = request()->param('pid');
            $arr_cate = $cate->field('id,cate_name')->where('pid',$pid)->select();
            if ($arr_cate){
                abort(200,'查询成功',['data'=>$arr_cate]);
            }
            abort(2001,'查询失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage(),$exception->getHeaders());
        }
    }

    public function findBrand (Brand $brand){
        try {
            $cate_id = request()->param('cate_id');
            $arr_brand = $brand->field('id,name')->where('cate_id',$cate_id)->select();
            if ($arr_brand){
                abort(200,'查询成功',['data'=>$arr_brand]);
            }
            abort(2001,'查询失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage(),$exception->getHeaders());
        }
    }

    public function getSpecAttr (Spec $spec,SpecValue $specValue,Attr $attr){
        try {
            $typeId = request()->param('type_id');
            $specs = $spec->where('type_id',$typeId)->select();
            $specs['spec_values'] = $specValue->where('type_id',$typeId)->select();
            $attrs = $attr->where('type_id',$typeId)->select();
            if ($specs && $attrs){
                abort(200,'查询成功',['attrs'=>$attrs,'specs'=>$specs]);
            }
            abort(2001,'查询失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage(),$exception->getHeaders());
        }
    }
}
